#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

void print_memory_usage(const Mat &img, const string &name)
{
    size_t bytes = img.total() * img.elemSize();
    cout << name << "内存占用：" << bytes / 1024.0 << " KB (" << bytes / (1024.0 * 1024.0) << " MB) ";
    cout << "分辨率：" << img.cols << "x" << img.rows << endl;
}

int main()
{
    // 读取图片
    Mat img1 = imread("../images/sumu001.jpg");
    Mat img2 = imread("../images/sumu011.jpg");

    // 检查图片是否加载成功
    if (img1.empty() || img2.empty())
    {
        cerr << "错误：无法读取图片文件" << endl;
        return -1;
    }

    // 打印原始内存占用
    cout << "== 原始图片内存 ==\n";
    print_memory_usage(img1, "图片1");
    print_memory_usage(img2, "图片2");

    // 横向拼接
    if (img1.rows == img2.rows)
    {
        Mat horizontal;
        hconcat(img1, img2, horizontal);

        cout << "\n== 横向拼接结果 ==\n";
        print_memory_usage(horizontal, "横向拼接图片");
        imwrite("horizontal_merged.jpg", horizontal);
    }
    else
    {
        cerr << "\n横向拼接失败：高度不一致 (" << img1.rows << " vs " << img2.rows << ")\n";
    }

    // 竖向拼接
    if (img1.cols == img2.cols)
    {
        Mat vertical;
        vconcat(img1, img2, vertical);

        cout << "\n== 竖向拼接结果 ==\n";
        print_memory_usage(vertical, "竖向拼接图片");
        imwrite("vertical_merged.jpg", vertical);
    }
    else
    {
        cerr << "\n竖向拼接失败：宽度不一致 (" << img1.cols << " vs " << img2.cols << ")\n";
    }

    return 0;
}